layui.config({
    base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', 'tree'], function () {
    $ = layui.jquery;
    $.get("/courseCate/all", function (res) {
        if (res.code == "00000") {
            var cates = res.data;
            var nodes = new Array();
            buidTree(nodes, cates);
            layui.tree({
                elem: "#cateTree",
                nodes: nodes,
                click: comfirm
            });
        }
    });

    function buidTree(container, cates) {
        for (var i = 0; i < cates.length; i++) {
            var cate = cates[i];
            var node = {
                id: cate.id,
                name: cate.cateName,
                pId: cate.parentId,
                catePath: cate.catePath,
                isSubCate: true
            };
            if (cate.childs.length > 0) {
                var childs = new Array();
                buidTree(childs, cate.childs);
                node.children = childs;
                node.isSubCate = false;
            }
            container.push(node);
        }
    }

    function comfirm(node) {
        var result = parent.cateTreeCallBack(node);
        if (result.isClose) {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        } else {
            result.callback();
        }
    }

});